#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         ModelLockingAccessory.py
#
# Copyright:    Copyright (c) 2005, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the ModelLockingAccessory class,
#               that provides a mechanism to lock and unlock
#               Models.
#
# See Also:     Accessory, Model
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Accessory import *
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *
from Products.KnowledgeEngine.WizardStepDefinition import *

#####################################################################
## Zope Library Imports
from Globals import InitializeClass
from AccessControl import ClassSecurityInfo
from AccessControl.Role import RoleManager
 
#####################################################################
## Python Library Imports
# (none)

#####################################################################
## ModelLockingAccessory Class Definition
class ModelLockingAccessory(Accessory):
    """
        An interface specifying the standard features of an Accessory
    """

    #---------------------------------------------------------------#

    #define security
    security = ClassSecurityInfo()
    security.declareObjectPublic()
    security.setDefaultAccess("allow")
        
    #---------------------------------------------------------------#

    def getIdentity(self):
        """
            ABSTRACT: Return the unique identity of the accessory
            in the Knowledge Engine
        """
        
        #typically this will be the same as the class name
        return "ModelLockingAccessory"
        
    #---------------------------------------------------------------#
        
    def getTitle(self, genericObject):
        """
            Returns the title of the Accessory (when
            applied to the specified genericObject)
            
            RETURN TYPE: String
        """

        #return different titles based on the locked status of the model
        if genericObject.isLockedForEditing():
            return "Unlock Model"
        else:
            return "Lock Model"
        
    #---------------------------------------------------------------#
        
    def getPurpose(self, genericObject):
        """
            ABSTRACT: Returns the purpose of the Accessory (when
            applied to the specified genericObject)
            
            RETURN TYPE: String
        """

        #return different purpose descriptions based on the locked status of the model
        if genericObject.isLockedForEditing():
            return "Permit property editing and component changes for this Model."
        else:
            return "Disable property editing and component changes for this Model.  This will additionally allow performance optimizations not available for unlocked models."
        
    #---------------------------------------------------------------#
        
    def getIconName(self, genericObject):
        """
            ABSTRACT: Returns the name of the Icon of the Accessory (when
            applied to the specified genericObject).
            
            The IconName represents the image file name that will be found using
            acquisition.
            
            RETURN TYPE: String
        """
        
        return "lock_icon.gif"
        
    #---------------------------------------------------------------#
        
    def getExecuteActionDisplayName(self, genericObject):
        """
            ABSTRACT: Returns the display name for the execute action of the Accessory (when
            applied to the specified genericObject)
            
            RETURN TYPE: String
        """

        #return different action names based on the locked status of the model
        if genericObject.isLockedForEditing():
            return "Unlock Model"
        else:
            return "Lock Model"
        
    #---------------------------------------------------------------#
        
    def isAvailableForGenericObject(self, genericObject):
        """
            Returns if the accessory is available for the 
            specified genericObject.
            
            This method is called to determine if an Accessory
            should be displayed for the specified genericObject 
            when rendering it's property editor etc.

            RETURN TYPE: Boolean
        """

        return genericObject.isInstanceOf("Model")
        
    #---------------------------------------------------------------#
        
    def executeAccessoryWith(self, genericObject, wizardState):
        """
            ABSTRACT: Executes the accessory for the specified
            genericObject, using the specified wizardState.            
            
            Returns a pair indicating the success of the accessory on
            the component and a corresponding message.
            
            RETURN TYPE: (Boolean, String)
        """
        
        #return different action names based on the locked status of the model
        if genericObject.isLockedForEditing():
            genericObject.unlockForEditing()
            message = "Model Successfully Unlocked. Model components and properties may now be edited."
        else:
            message = "Model Successfully Locked. Model components and properties may not be edited."
            genericObject.lockForEditing()

        #locking/unlocking is always successful
        return (true, message)
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Accessory Information with the Knowledge Engine
registerAccessory(ModelLockingAccessory())

#####################################################################
